package com.bdqn.demo03;

import org.apache.log4j.Logger;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * JDKInvocationHandler
 *
 * @author LILIBO
 * @since 2021-12-02
 */
public class JDKInvocationHandler implements InvocationHandler {

    private static final Logger logger = Logger.getLogger(JDKInvocationHandler.class);


    /**
     * 被代理的目标对象
     */
    private Object target;

    public void setTarget(Object target) {
        this.target = target;
    }

    /**
     * 为被代理的接口定义的代理业务规则（实现增强）
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        long start = System.currentTimeMillis();
        logger.debug("正在调用" + target.getClass().getName() + "类的" + method.getName() + "方法，参数是：" + args);

        Object result = method.invoke(target, args);

        long end = System.currentTimeMillis();
        logger.debug(target.getClass().getName() + "类的" + method.getName() + "方法调用完成（"+(end - start)+"毫秒），返回值是：" + result);
        return result;
    }
}
